set scheme s1mono 
********************************************************************************************
*Figure 1: Geographic Distribution of Subsidy-Giving and Incentive Spending 
********************************************************************************************
****** insheet BDS data for entry stats
insheet using $datadir/raw/bds_size_state.csv, comma clear

keep if regexm(size, "h") | regexm(size, "i") | regexm(size, "g") | regexm(size, "f")

keep year state size estabs estabs_entry estabs_exit job_creation job_creation_births
drop if year<2000

g emp=substr(size,4,.)
replace emp=subinstr(emp, " to ", "_", .)
replace emp=subinstr(emp, "+", "", .)

keep year state emp estabs estabs_entry job_creation job_creation_births

reshape wide estabs estabs_entry job_creation job_creation_births, i(state year) j(emp) string

rename job_creation* jc*
rename year2 year
rename state fips

****** just need 100+ establishment entry
egen new_estabs100=rowtotal(estabs_entry*)
keep year fips new_estabs100

statastates, fips(fips) nogen 

rename state_* state*

****** Merge to get state level spending
merge 1:1 stateabbrev year using $datadir/raw/state_level_spending, keep(3) nogen

rename stateabbrev state
keep if inrange(year, 2007,2014)

****** Total economic development spending at the state level
egen total_spending = rowtotal(total_credits total_budget)
drop *_credits *_budget

****** Adjust for inflation
adjust_inflation total_spending, year(2017) 

****** Maps 
g spend_per_entry = total_spend/(new_estabs100+1)
keep if inrange(year, 2007,2014)
collapse (mean) total*  spend_per*, by(state fips statename)
g map_spend = total_spend/1000000

replace map=. if map<50
maptile map, geo(state) fcolor(Greys) cutv(100 200 300 500) ///
	ndf(white) twopt(legend(order(0 "average ({c $|}M)" 1 "<50" ///
	2 "50-100" 3 "100-200" 4 "200-300" 5 "300-500" 6 "500+")))
graph export $draftdir/spend_map.eps, replace

g entry_map=spend_per_entry/1000000
replace entry_map = . if entry_map<3
maptile entry_map, geo(state) fcolor(Greys) cutv(5.1 7 9 15) ndf(white)  ///
	twopt(legend(order(0 "per-entrant ({c $|}M)" 1 "0-2" ///
	2 "3-4" 3 "5-6" 4 "7-8" 5 "9-15" 6 "16+")))	
graph export $draftdir/spend_entry_map.eps, replace

***** Map with just the number of subsidies over sample
u $datadir/subsidy_data, clear
g n=1
keep id n state
duplicates drop 
collapse (sum) n, by(state)

maptile n, geo(state) fcolor(Greys) ndf(white) n(5) ///
	twopt(legend(order(1 "0" 2 "1-3" 3 "4-7" 4 "8-12" 5 "12-19" 6 "20+")))
graph export $draftdir/n_sub_map.eps, replace 

u $datadir/subsidy_data, clear
g n=1
keep if naics2==31
keep id n state
duplicates drop 
collapse (sum) n, by(state)

maptile n, geo(state) fcolor(Greys) ndf(white) cutv(1.5 3.5 5.5 10.5) ///
	twopt(legend(order(1 "0" 2 "1" 3 "2-3" 4 "4-5" 5 "6-10" 6 "11+")))
graph export $draftdir/map_win_manuf.eps, replace 

***** Map with average cost per job per state
u $datadir/subsidy_data, clear
keep sub_M jobs_total state id 
duplicates drop 
collapse (mean) sub_M jobs_total, by(state)
g cost_per = sub_M*1000000/jobs_total

replace cost_per = cost_per/1000
maptile cost_per, geo(state) fcolor(Greys) ndf(white) cutv(50 75 100 200) ///
	twopt(legend(order(2 "<$50k" 3 "$50-75k" 4 "$75-100k" 5 "$100-200k" 6 "$200k+")))
graph export $draftdir/cost_per_map.eps, replace 

********************************************************************************************
*Figure 3: Identification via Runner-up
********************************************************************************************

u $datadir/analysis_cz, clear
keep if ru_limit==1
//same sample restrictions as main analysis
drop if sub_M>1000 

*corporate tax rate
binscatter sub_M diff_corp ,   control( diff_perc_est diff_FHFA  diff_income_tax diff_pr_college ///
	jobs_direct invest_M corp_tax income_tax unemp ln_pinc ) ///
	m(Oh) mcolor(black) lcolor(gray) ///
	ytitle("subsidy ({c $|}M)") xtitle("difference in corporate tax rate (%)") ///
	ylabel(0(50)200) xlabel(-5(1)5)
graph export $draftdir/bs_subcorp.eps, replace

*auto network 
binscatter sub_M diff_auto,  control(diff_corp diff_perc_est diff_FHFA  diff_income_tax diff_pr_college ///
	jobs_direct invest_M corp_tax income_tax  unemp ln_pinc ) ///
	m(Oh) mcolor(black) lcolor(gray)  ///
	ytitle("subsidy ({c $|}M)") xtitle("difference in auto road network") 	///
	xlabel(-1(.2)1) ylabel(0(50)200) 
graph export $draftdir/bs_autonetwork.eps, replace

*unemployment rate
binscatter sub_M unemp , control(diff_corp diff_perc_est diff_FHFA  diff_income_tax diff_pr_college ///
	jobs_direct invest_M corp_tax income_tax ln_pinc) ///
	m(Oh) mcolor(black) lcolor(gray)  ///
	ytitle("subsidy ({c $|}M)") xtitle("unemployment rate (%)") ///
	ylabel(50(50)200) 
graph export $draftdir/bs_subunemp.eps, replace
	
*personal income
binscatter sub_M ln_pinc, control(diff_corp diff_perc_est diff_FHFA ///
	jobs_direct invest_M diff_r2w corp_tax income_tax diff_income_tax diff_pr_college unemp) ///
	m(Oh) mcolor(black) lcolor(gray) ///
	ytitle("subsidy ({c $|}M)") xtitle("log(personal income per capita, {c $|}1,000)") ///
	ylabel(50(50)200) xlabel(3.5(.1)4.2)
graph export $draftdir/bs_subincome.eps, replace

********************************************************************************************
*Figure 4: Model Fit: Runner-up Subsidy Offers and Predicted Valuations 
********************************************************************************************
u $datadir/analysis_cz, clear
keep id_win ru_offer jobs_direct invest_B sub_M mult_tot manuf
duplicates drop 
drop if ru_offer==.
sa $temp/ru_offer, replace 

insheet using $datadir/runner_up_correlations_serv.csv, comma clear
g manuf=0
sa $temp/s, replace
insheet using $datadir/runner_up_correlations.csv, comma clear
g manuf=1 
append using $temp/s

rename v2 profits_ru_cond
rename v3 profits_ru
rename v4 v_ru
rename v5 v_ru_cond
rename v6 v_ru_cond_resid
rename v7 v_ru_resid
rename v8 id_win 

drop v1 

merge 1:m id_win using $temp/ru_offer, keep(3) nogen

tw (scatter v_ru_resid ru_offer, m(oh)) (line ru_offer ru_offer), ylabel(0(250)1500) xlabel(0(250)1500) ///
 legend(off) ytitle("predicted runner-up valuation ({c $|}M)") xtitle("runner-up subsidy offer ({c $|}M)")
 graph export $draftdir/offer_v.eps, replace

*******************************************************************************************
*Figure 5: Runner up pi and v (Step 1)
*******************************************************************************************
insheet using $datadir/runner_up_correlations_serv.csv, comma clear

keep v2 v6 
rename v2 profits_ru_cond
rename v6 v_ru_cond_resid
g manuf=0

sa $temp/ru_serv, replace

insheet using $datadir/runner_up_correlations.csv, comma clear

keep v2 v6 
rename v2 profits_ru_cond
rename v6 v_ru_cond_resid
g manuf=1 

append using $temp/ru_serv 

binscatter profits v if manuf==0 , m(oh) lcol(gray) xtitle("Valuation ({c $|}M)") ytitle("Profits ({c $|}M)")
		graph export $draftdir/bs_serv.eps, replace
		
binscatter profits v if manuf==1, m(oh) lcol(gray) xtitle("Valuation ({c $|}M)") ytitle("Profits ({c $|}M)")
		graph export $draftdir/bs_manuf.eps, replace
	
********************************************************************************
*Figure 6: Model Fit: Simulated Subsidy Competitions
********************************************************************************
* FIGURE 6(a) 
********************************************************************************

	insheet using $datadir/sim_sub.csv, comma clear 
	replace sim_sub=0 if sim_sub<0 
	g mean_sub = sim_sub 
	collapse(p50)  sim_sub (mean) mean_sub , by(id_deal sub_m) 
	g manuf=1 
	sa $temp/manuf, replace 

	insheet using $datadir/sim_sub_serv.csv, comma clear 
	replace sim_sub=0 if sim_sub<0 
	g mean_sub = sim_sub 
	collapse (p50)  sim_sub (mean) mean_sub , by(id_deal sub_m) 
	g manuf=0

	append using $temp/manuf 

twoway (histogram sub_m, bin(50)) (kdensity sim_sub, lw(thick) lpattern(dash)), legend(order(1 "data" 2 "simulated") col(2)) xtitle("Subsidy ({c $|}M)")
graph export $draftdir/model_fit_whist.eps, replace 

********************************************************************************
* Figure 6(b) [this panel is a table]
****************************************************************************
preserve 
rename sub_m x

collapse  (mean) mean= x (p25) p25 = x (p10) p10 = x ///
	(p75) p75= x (p50) p50 = x (p90) p90 = x
	
	rename * data_*
	g v1=1
	
sa $temp/data, replace
restore 

preserve 
rename sim_sub x

collapse  (mean) mean= x (p25) p25 = x (p10) p10 = x ///
	(p75) p75= x (p50) p50 = x (p90) p90 = x
	
	rename * sim_*
	g v1=1
	
sa $temp/sim, replace
restore 

rename mean_sub x

collapse  (mean) mean= x (p25) p25 = x (p10) p10 = x ///
	(p75) p75= x (p50) p50 = x (p90) p90 = x
	
	rename * sim2_*
	g v1=1
	

merge 1:1 v1 using $temp/data, nogen 
merge 1:1 v1 using $temp/sim , nogen 

reshape long sim_ sim2_ data_ , i(v1) string 

format sim_ sim2_ data_ %12.2fc

g v_label = _j 
foreach v in 10 25 50 75 90 {
		replace v_label = "`v'$^{th}$" if _j == "p`v'"
}

replace v_label = "Mean" if _j == "mean"
sort v_label 

	* MAKE TABLE
	g tab = "\begin{tabular}{l|rrr}" in 1
	g top = "\toprule" in 1
	*Midrule
	g mid = " \midrule" in 1 
	g hline = " \hline" in 1 
	*Bottomrule
	g bot = "\bottomrule" in 1
	*End
	g end = "\end{tabular}" in 1
	*Panel

		g title1 = "  &  & \multicolumn{2}{c}{Simulated} \\ " in 1
		
		g title2 = " Pctile & Data & Med. & Mean \\ " in 1
	g c = " "
	g space =  " \multicolumn{1}{c}{} & & & \\ " 

*****Output
	local filename = "modelfit"

	listtex tab if _n == 1 using "$draftdir/`filename'.tex", replace rstyle(none)	
		listtex title1 if _n == 1, appendto("$draftdir/`filename'.tex") rstyle(none)	
		listtex title2 if _n == 1, appendto("$draftdir/`filename'.tex") rstyle(none)	
		listtex hline if _n == 1, appendto("$draftdir/`filename'.tex") rstyle(none)
	listtex v_label data sim_ sim2_  ///
		if _j!="mean", appendto("$draftdir/`filename'.tex") rstyle(tabular) 	
			listtex hline if _n == 1, appendto("$draftdir/`filename'.tex") rstyle(none)
	listtex v_label data sim_ sim2_  ///
		if _j=="mean", appendto("$draftdir/`filename'.tex") rstyle(tabular) 
	listtex hline if _n == 1, appendto("$draftdir/`filename'.tex") rstyle(none)
	listtex space if _n == 1, appendto("$draftdir/`filename'.tex") rstyle(none)
	listtex end if _n == 1, appendto("$draftdir/`filename'.tex") rstyle(none)	 

	********************************************************************************************
* FIGURE 7: Counterfactual Changes in Firm Locations 
********************************************************************************************
 u $datadir/analysis_cz, clear
 keep if manuf==1 & sub_M<1000
 keep id_win stateabbrev_win 
 duplicates drop 
 g n = 1 
 collapse (sum) n, by(stateabbrev) 
 rename stateabbrev stateabbrev
sa $temp/n_wins, replace 
 
insheet using $datadir/cf_locations.csv, comma clear 
g sim_win = share/100
collapse (sum ) sim_win, by(stateabbrev)
merge 1:1 stateabbrev using $temp/n_wins

rename stateabbrev state

	maptile n, fcolor(Greys) geo(state)  ///
	    ndf(white) cutv(1.5 2.5 4.5 9.5) twopt(legend(order(0 "subsidy wins:" 1 "0" 2 "1" 3 "2" 4 "3-4" 5 "5-9" 6 "10+"))) 
	graph export $draftdir/map_real_manuf.eps, replace	
	
recode n(.=0)
		
	g diff = sim - n
	g round_diff = round(diff)

	maptile diff, fcolor(Greys) geo(state) ///
	    ndf(white) cutv(-2.5 -.5 .5 2.5) ///
		twopt(legend(order(0 "change:"  1 "no data" 2 "lose 3+" ///
		3 "lose 1-2" 4 "no change" 5 "gain 1-2" 6 "gain 3+" )))
	graph export $draftdir/map_counter_manuf.eps, replace	

 u $datadir/analysis_cz, clear
 keep if manuf==0 & sub_M<400
 keep id_win stateabbrev_win 
 duplicates drop 
 g n = 1 
 collapse (sum) n, by(stateabbrev) 
 rename stateabbrev stateabbrev
sa $temp/n_wins, replace 
 
insheet using $datadir/cf_locations_serv.csv, comma clear 
g sim_win = share/100
collapse (sum ) sim_win, by(stateabbrev)
merge 1:1 stateabbrev using $temp/n_wins

rename stateabbrev state

	maptile n, fcolor(Greys) geo(state) ///
	    ndf(white) cutv(1.5 2.5 4.5 9.5) twopt(legend(order(0 "subsidy wins:" 1 "0" 2 "1" 3 "2" 4 "3-4" 5 "5-9" 6 "10+")))
	graph export $draftdir/map_real_serv.eps, replace	
	
recode n(.=0)
		
	g diff = sim - n
	g round_diff = round(diff)
	recode diff(.=0)

	maptile diff, fcolor(Greys) geo(state)  ///
    ndf(white) cutv(-2.5 -.5 .5 2.5) ///
		twopt(legend(order(0 "change:" 1 "no data" 2 "lose 3+" ///
		3 "lose 1-2" 4 "no change" 5 "gain 1-2" 6 "gain 3+")))
	graph export $draftdir/map_counter_serv.eps, replace	